package com.qdc.datacenters.controller;

import com.github.pagehelper.PageInfo;
import com.qdc.datacenters.annotation.Log;
import com.qdc.datacenters.domain.ResponseResult;
import com.qdc.datacenters.domain.entity.CorpPermitUser;
import com.qdc.datacenters.domain.query.BaseQuery;
import com.qdc.datacenters.domain.query.user.CorpPermitUserQuery;
import com.qdc.datacenters.enums.BusinessTypeEnum;
import com.qdc.datacenters.service.ICorpPermitUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author danmo
 * @date 2024-03-13 14:50
 **/
@Tag(name = "存档范围")
@RestController
@RequestMapping("/permit")
public class CorpPermitUserController extends BaseController {

    @Autowired
    private ICorpPermitUserService iCorpPermitUserService;

    @Log(title = "同步授权范围员工", businessType = BusinessTypeEnum.INSERT)
    @PreAuthorize("@authz.hasPerm('permit:sync')")
    @Operation(summary = "同步授权范围员工", method = "POST")
    @PostMapping("/sync")
    public ResponseResult sync(@RequestBody BaseQuery query) {
        iCorpPermitUserService.sync(query);
        return ResponseResult.success();
    }


    @Log(title = "范围员工列表", businessType = BusinessTypeEnum.SELECT)
    @PreAuthorize("@authz.hasPerm('permit:list')")
    @Operation(summary = "范围员工列表", method = "POST")
    @PostMapping("/list")
    public ResponseResult<PageInfo<CorpPermitUser>> list(@RequestBody CorpPermitUserQuery query) {
        startPage(query.getPageIndex(), query.getPageSize(), query.getSortField(), query.getSort());
        List<CorpPermitUser> list = iCorpPermitUserService.getList(query);
        PageInfo<CorpPermitUser> pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }

    @Log(title = "修改范围员工状态", businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@authz.hasPerm('permit:update:status')")
    @Operation(summary = "修改范围员工状态", method = "POST")
    @PostMapping("/updateStatus")
    public ResponseResult updateStatus(@RequestBody CorpPermitUserQuery query) {
        iCorpPermitUserService.updateStatus(query.getId(),query.getStatus());
        return ResponseResult.success();
    }

    @Log(title = "删除范围员工", businessType = BusinessTypeEnum.DELETE)
    @PreAuthorize("@authz.hasPerm('permit:delete')")
    @Operation(summary = "删除范围员工", method = "POST")
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody CorpPermitUserQuery query) {
        iCorpPermitUserService.delete(query);
        return ResponseResult.success();
    }
}
